home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8656 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: news.bridge.net!news
  2. From: David Byrden <100101.2547@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: modifying a 'const' variable, a bug in VC++?
  5. Date: 25 Feb 1996 21:58:32 GMT
  6. Organization: self-employed
  7. Message-ID: <4gqm28$ci3@news.bridge.net>
  8. References: <00001a81+0000a7f1@msn.com>
  9. NNTP-Posting-Host: ppp-mia1-43.bridge.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15.  
  16.  
  17. >>>>>>>
  18.         const int i = 1;
  19.         int* j = (int *)&i;     // cast a "const int*" to "int*". The
  20.                                         // compiler should allow it
  21.  
  22.         *j = 2;
  23.         cout << *j << newl;     // should print "2"
  24.         cout << i << newl;      // should print "2" too, prints 1
  25.  
  26.  
  27. I consider this a bug in VC++(The correct result should be 2\n2\n.
  28. Any comments? 
  29.  
  30. <<<<<<<
  31.  
  32.  
  33.   You can't complain. Casting away constancy, as you did, is not valid 
  34. unless the original object referred to is really not const. Your variable 
  35. "i" is const and so the compiler is free to assume that it will never 
  36. change under any circumstances.
  37.  
  38.  
  39.                                      David
  40.  
  41.  
  42.